import 'package:binggo_bomb/commons/CardLocalStorageWidget.dart';
import 'package:binggo_bomb/commons/Constants.dart';
import 'package:binggo_bomb/page/card/CardInfoWidget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class CardListWidget extends StatefulWidget {
  final int color;
  final Map nums;
  final Function delCard;
  const CardListWidget({super.key, required this.color, required this.nums, required this.delCard});

  @override
  State<CardListWidget> createState() => _CardListWidgetState();
}

class _CardListWidgetState extends State<CardListWidget> {

  /// 卡片组件
  List<Widget> cards = [];

  @override
  void initState() {
    super.initState();
    buildCards();
  }

  @override
  void didUpdateWidget(CardListWidget oldWidget) {
    if(oldWidget.nums != widget.nums) {
      buildCards();
    }
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 160,
      child: SingleChildScrollView(
        child: Column(
            children: cards
        ),
      ),
    );
  }



  /// 构建卡片组件
  buildCards () {
    List<Widget> ws = [];
    int page = 1;
    widget.nums.keys.forEach((key) {
      ws.add(
          InkWell(
            child: Container(
              width: 260,
              height: 250,
              margin: EdgeInsets.only(top: 15, bottom: 15),
              child: Column(
                children: [
                  Container(
                    width: 210,
                    height: 210,
                    // color: Colors.purpleAccent,
                    child: Card(
                      color: Colors.black,
                      elevation: 4,
                      shape: RoundedRectangleBorder(),
                      child: Container(
                        margin: EdgeInsets.only(left: 1, top: 1,right: 1, bottom: 1),
                        child: GridView.count(
                          crossAxisCount: 5,
                          mainAxisSpacing: 1,
                          crossAxisSpacing: 1,
                          children: buildItem(widget.nums[key]),
                        ),
                      ),
                    ),
                  ),
                  Text("第$page张"),
                ],
              ),
            ),
            onTap: () {
              Navigator.of(context).push(
                  MaterialPageRoute(
                      builder: (_) =>
                          CardInfoWidget(cardKey: key.toString(), nums: widget.nums[key.toString()], color: widget.color, onChang: (color, key, nums) {
                            childChange(color, key, nums);
                          })
                  )
              );
            },
            onLongPress: () {
              optCard(key);
            },

          )
      );

      page++;
    });
    setState(() {
      cards = ws;
    });
  }




  /// 构建卡片数字阵列
  List<Widget> buildItem(List ids) {
    List<Widget> listWidgets = [];
    for(int i = 0;i < ids.length; i++) {
      listWidgets.add(
        Container(
          width: 10,
          height: 10,
          color: Constants.cardColors[widget.color],
          alignment: Alignment.center,
          child: Text( ids[i].toString(),
            style: TextStyle(
                color: Colors.black,
                fontSize: 24,
                fontWeight: FontWeight.bold
            ),
          ),
        ),
      );
    }
    return listWidgets;
  }

  /// 操作卡片
  optCard(String key) {
    showDialog(
        context: context,
        builder: (ctx) {
          return Container(
            alignment: Alignment.bottomCenter,
            child: CupertinoActionSheet(
              title: Text("操作"),
              // message: Text(""),
              cancelButton: CupertinoActionSheetAction(
                onPressed: () {
                  Navigator.pop(context);
                },
                child: Text("取消"),
              ),
              actions: [
                // CupertinoActionSheetAction(
                //   onPressed: () {
                //     print("编辑");
                //   },
                //   child: Text("编辑"),
                // ),
                CupertinoActionSheetAction(
                  onPressed: () {
                    widget.delCard(key);
                  },
                  child: Text("删除"),
                ),
              ],
            ),
          );
        }
    );
  }

  /// 编辑更新卡片数据信息
  childChange(color, key, nums) {
    /// 重新加载页面券卡片
    setState(() {
      widget.nums[key] = nums;
    });
    /// 更新缓存 粉色券 号码
    CardLocalStorageWidget.setCardNums(color, widget.nums);
    /// 重新构建列表卡片数据
    buildCards();
  }

}
